define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
	const controller = {
		index() {
			Table.api.init({
				extend: {
					index_url: 'subject/subject/index', //列表查询的请求控制器方法
					add_url: 'subject/subject/add', //添加的控制器地址
					edit_url: 'subject/subject/edit', //编辑的控制器地址
					del_url: 'subject/subject/del', //删除的控制器地址
					info_url: 'subject/info/index', // 课程详情
					chapter_url: 'subject/chapter/index', // 课程章节
					table: 'subject',
				},
			});

			const table = $('#table');

			table.bootstrapTable({
				// 请求地址
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				// 设置工具栏元素
				toolbar: '.toolbar',
				// 设置主键
				pk: 'id',
				// 排序字段
				sortName: 'createtime',
				// 默认排序方式
				sortOrder: 'desc',
				showExport: false,
				// 渲染表格数据
				columns: [
					{ checkbox: true },
					{ field: 'id', title: 'ID', operate: false, sortable: true },
					{ field: 'title', title: __('Titles'), operate: 'LIKE' },
					{ field: 'category.name', title: __('Cateid') },
					{ field: 'thumbs_text', title: __('Thumbs'), formatter: Table.api.formatter.image },
					{ field: 'price', title: __('Price'), sortable: true },
					{ field: 'likes_text', title: __('Likes') },
					{
						field: 'createtime',
						title: __('CreateTime'),
						sortable: true,
						operate: 'RANGE',
						addclass: 'datetimerange',
						formatter: Table.api.formatter.datetime,
					},
					{
						field: 'operate',
						title: __('Operate'),
						table: table,
						events: Table.api.events.operate,
						formatter: Table.api.formatter.operate,
						buttons: [
							// 查看详情按钮
							{
								name: 'info',
								title: (data) => `${data.title} - 课程章节`,
								icon: 'fa fa-list-ul', //图标
								classname: 'btn btn-xs btn-info btn-dialog',
								url: $.fn.bootstrapTable.defaults.extend.chapter_url,
								extend: `data-toggle='tooltip' data-area= '["90%", "90%"]'`,
							},
							// 查看详情按钮
							{
								name: 'info',
								title: (data) => `${data.title} - 课程详情`,
								icon: 'fa fa-file-text-o', //图标
								classname: 'btn btn-xs btn-info btn-dialog',
								url: $.fn.bootstrapTable.defaults.extend.info_url,
								extend: `data-toggle='tooltip' data-area= '["90%", "90%"]'`,
							},
						],
					},
				],
			});

			Table.api.bindevent(table);
		},
		add() {
			controller.api.bindevent();
		},
		edit() {
			controller.api.bindevent();
		},
		del() {
			controller.api.bindevent();
		},
		api: {
			//JS模块化的全局方法
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};

	return controller;
});
